import 'package:flutter/material.dart';
import 'package:flutter_09_game/base/widget/base_list_more_get_builder.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/pages/deposit/models/model_record_recharge.dart';
import 'package:flutter_09_game/pages/deposit/widgets/widget_recharge_detail.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_09_game/utils/extensions/num_ext.dart';
import 'package:flutter_09_game/utils/utils_other.dart';
import 'package:flutter_09_game/utils/utils_time.dart';
import 'package:flutter_09_game/widgets/btn_dropdown.dart';
import 'package:flutter_09_game/widgets/crypto_tag_selector.dart';
import 'package:flutter_09_game/widgets/dialog/common_normal_sheet.dart';
import 'package:flutter_09_game/widgets/empty/empty_no_data.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'deposit_record_withdraw_logic.dart';

class DepositRecordWithdrawPage extends StatelessWidget {
  DepositRecordWithdrawPage({super.key});

  final DepositRecordWithdrawLogic logic =
      Get.put(DepositRecordWithdrawLogic());

  @override
  Widget build(BuildContext context) {
    return BaseListMoreGetBuilder<DepositRecordWithdrawLogic>(
      emptyBuilder: () {
        return SizedBox(
          width: 1.sw,
          child: Padding(
            padding: EdgeInsets.only(top: 108.w),
            child: EmptyNoData.notDataView(),
          ),
        );
      },
      bodyTopViewBuilder: (l) {
        return Column(
          children: [
            Obx(() {
              return Container(
                color: AppColor.navBgColor,
                padding: EdgeInsets.all(16.w).copyWith(bottom: 0),
                child: CryptoTagSelector(
                  options: [
                    LocaleKeys.withdrawAll.tr,
                    LocaleKeys.commissionWithdrawal.tr
                  ],


                  selectedValue: logic.selectorTagRx.value,
                  onChanged: (value) {
                    logic.selectorTagRx.value = value;
                  },
                ),
              );
            }),
            // 16.verticalSpaceFromWidth,
            Container(
              padding: EdgeInsets.all(16.w).copyWith(bottom: 24.w),
              decoration: BoxDecoration(
                color: AppColor.navBgColor,
                borderRadius: BorderRadius.only(
                  bottomLeft: Radius.circular(30.w),
                  bottomRight: Radius.circular(30.w),
                ),
              ),
              child: Obx(() {
                return Row(
                  children: [
                    Expanded(
                        child: _buildTopDropdown([
                      LocaleKeys.allCoins.tr,
                      ...AppUserManager().walletList.map((f) => f.symbol ?? '')
                    ],
                            value: logic.symbolRx.value.isEmpty
                                ? LocaleKeys.allCoins.tr
                                : logic.symbolRx.value, onChanged: (value) {
                      if (value == LocaleKeys.allCoins.tr) {
                        logic.symbolRx.value = '';
                      } else {
                        logic.symbolRx.value = value;
                      }
                    })),
                    4.horizontalSpace,
                    Expanded(
                        child: _buildTopDropdown([
                      LocaleKeys.allStatus.tr,
                      LocaleKeys.withdrawSuccess.tr,
                      LocaleKeys.waitingForArrival.tr,
                      LocaleKeys.withdrawFailed.tr,
                    ], value: logic.statusRx.value, onChanged: (value) {
                      logic.statusRx.value = value;
                    })),
                    4.horizontalSpace,
                    Expanded(
                        child: _buildTopDropdown([
                      LocaleKeys.today.tr,
                      LocaleKeys.yesterday.tr,
                      LocaleKeys.threeDays.tr,
                      LocaleKeys.sevenDays.tr,
                      LocaleKeys.custom.tr,
                    ], btnColor: AppColor.primary, value: logic.timeRx.value,
                            onChanged: (value) {
                      // logic.timeRx.value = value;
                      logic.updateTimeSelection(value);
                    })),
                  ],
                );
              }),
            ),
          ],
        );
      },
      builder: (_) {
        return SingleChildScrollView(
            padding: EdgeInsets.only(bottom: 16.w),
            child: Container(
              margin:
                  EdgeInsets.symmetric(horizontal: 16.w).copyWith(top: 16.w),
              padding: EdgeInsets.all(12.w),
              decoration: BoxDecoration(
                color: AppColor.navBgColor,
                borderRadius: BorderRadius.circular(16.w),
              ),
              child: Column(
                children: [
                  Row(
                    children: [
                      _buildHeaderCell(LocaleKeys.time.tr, 94.w),
                      8.horizontalSpace,
                      _buildHeaderCell(LocaleKeys.currency.tr, 60.w),
                      8.horizontalSpace,
                      Expanded(child: _buildHeaderCell('金额', null)),
                      8.horizontalSpace,
                      _buildHeaderCell('状态', 72.5.w),
                    ],
                  ),

                  8.verticalSpaceFromWidth,
                  ...logic.list.map((value) {
                    ModelRecordRecharge record = value;
                    return Container(
                      margin: EdgeInsets.only(bottom: 4.w),
                      child: Row(
                        children: [
                          Container(
                            width: 94.w,
                            height: 40.w,
                            // color: Colors.red,
                            alignment: Alignment.centerLeft,
                            child: Text(
                              UtilsTime.depositFormatTime(record.createdAt),
                              style: TextStyle(
                                color: AppColor.textPrimary.withOpacity(0.7),
                                fontSize: 12.sp,
                                fontWeight: FontWeight.w400,
                              ),
                            ),
                          ),
                          8.horizontalSpace,
                          SizedBox(
                            width: 60.w,
                            height: 40.w,
                            child: Row(
                              children: [
                                AutoImage(
                                  UtilsOther().getAmountTagIcon(record.symbol),
                                  width: 16.w,
                                  height: 16.w,
                                ),
                                4.horizontalSpace,
                                Expanded(
                                  child: Text(
                                    record.symbol ?? '',
                                    style: TextStyle(
                                      color:
                                          AppColor.textPrimary.withOpacity(0.7),
                                      fontSize: 12.sp,
                                      fontWeight: FontWeight.w400,
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                          8.horizontalSpace,
                          Expanded(
                              child: Container(
                            height: 40.w,
                            alignment: Alignment.centerLeft,
                            child: Text(
                              "+${record.amount?.formatWallet() ?? ''}",
                              style: TextStyle(
                                color: AppColor.textPrimary,
                                fontSize: 12.sp,
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                          )),
                          8.horizontalSpace,
                          GestureDetector(
                            onTap: () {
                              // record.status = 4;
                              CommonNormalSheet(
                                title: LocaleKeys.withdrawDetail.tr,
                                child: WidgetRechargeDetail(
                                  record: record,
                                  isWithdraw: true,
                                ),
                              ).show();
                            },
                            child: SizedBox(
                              width: 72.5.w,
                              height: 40.w,
                              child: Row(
                                children: [
                                  Expanded(
                                    child: Text(
                                      _getStatusText(record.status),
                                      //   _getStatusText(record.status),
                                      style: TextStyle(
                                        color: _getStatusColor(record.status),
                                        // color: _getStatusColor(record.status),
                                        fontSize: 12.sp,
                                        fontWeight: FontWeight.w400,
                                      ),
                                    ),
                                  ),
                                  4.horizontalSpace,
                                  AutoImage(
                                    Assets.mineRigthtRight,
                                    width: 20.w,
                                    height: 20.w,
                                    color:
                                        AppColor.textPrimary.withOpacity(0.5),
                                  ),
                                ],
                              ),
                            ),
                          )
                        ],
                      ),
                    );
                  })
                  // if (records.isNotEmpty) _buildNoMoreData(),
                ],
              ),
            ));
      },
    );
    // return Container();
  }

  Widget _buildTopDropdown(List<String> items,
      {required String value,
      required Function(String) onChanged,
      Color? btnColor}) {
    return BtnDropdown(
        items: items,
        value: value,
        // width: 1.sw - 32.w,
        // mainAxisAlignment: MainAxisAlignment.start,
        onChanged: (value) {
          onChanged.call(value);
        },
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 12.w),
          decoration: BoxDecoration(
              border: Border(
                  bottom: BorderSide(
                      color: AppColor.textPrimary.withOpacity(0.15), width: 1)),
              color: Colors.black.withOpacity(0.3),
              borderRadius: BorderRadius.circular(8.w)),
          child: Row(
            children: [
              Expanded(
                  child: Text(
                value,
                style: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w900,
                  color: btnColor ?? AppColor.textPrimary.withOpacity(0.94),
                ),
              )),
              AutoImage(
                Assets.promotionCommissionCaretDown,
                width: 16.w,
              )
            ],
          ),
        ));
  }

  Widget _buildHeaderCell(String title, double? width) {
    return Container(
      width: width,
      height: 40.w,
      alignment: Alignment.centerLeft,
      child: Text(
        title,
        style: TextStyle(
          color: AppColor.textPrimary.withOpacity(0.94),
          fontSize: 14.sp,
          fontWeight: FontWeight.w600,
        ),
      ),
    );
  }

  /// 根据状态获取对应的文案
  String _getStatusText(int? status) {
    switch (status) {
      case 3:
        return LocaleKeys.withdrawSuccess.tr; // 提现成功
      case 1:
      case 2:
        return LocaleKeys.waitingForArrival.tr; // 等待到账
      case 4:
      case 5:
        return LocaleKeys.withdrawFailed.tr; // 提现失败
      default:
        return LocaleKeys.withdrawSuccess.tr; // 默认显示成功
    }
  }

  /// 根据状态获取对应的颜色
  Color _getStatusColor(int? status) {
    switch (status) {
      case 3:
        return AppColor.primarySecondary; // 成功 - 绿色
      case 1:
      case 2:
        return AppColor.gameWarningColor; // 等待 - 橙色
      case 4:
      case 5:
        return AppColor.gameErrorColor; // 失败 - 红色
      default:
        return AppColor.textPrimary.withOpacity(0.7); // 默认 - 灰色
    }
  }
}
